package com.gouwudai.gobbs.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;

import org.extremecomponents.table.limit.Limit;

import com.gouwudai.gobbs.common.Constants;
import com.gouwudai.gobbs.common.IPageList;
import com.gouwudai.gobbs.dao.IBusinessDAO;
import com.gouwudai.gobbs.dao.IUserDAO;
import com.gouwudai.gobbs.pojo.TbbsBusiness;
import com.gouwudai.gobbs.service.IBusinessService;
import com.litt.core.common.Utility;
import com.litt.core.exception.BusiException;
import com.litt.core.util.ExtremeTableUtil;
import com.litt.core.util.ParamUtils;

public class BusinessServiceImpl implements IBusinessService
{
	IBusinessDAO businessDAO;	
	IUserDAO userDAO;
	
	/**
	 * 用户交易
	 * @return
	 */
	public void doExchange(HashMap params) throws BusiException
	{
			int businessType = ParamUtils.getMapToInt(params, "businessType");
			Integer userId = new Integer(ParamUtils.getMapToInt(params, "userId"));
			BigDecimal businessNum = Utility.parseBigDecimal(ParamUtils.getMapToString(params, "businessNum"));	//交易数量
			BigDecimal businessRate = Utility.parseBigDecimal(ParamUtils.getMapToString(params, "businessRate")); //兑换比例
			BigDecimal zero = new BigDecimal(0);
			BigDecimal valueAdd = zero;  //增加的值
			BigDecimal valueReduce = zero; //减少的值
			switch(businessType)	
			{
				case 1:	//积分兑换金币
					valueAdd = businessNum.divide(businessRate).setScale(0,BigDecimal.ROUND_DOWN);  //增加的值
					valueReduce = zero.subtract(valueAdd.multiply(businessRate)); //减少的值
					break;
				case 2: //金币兑换积分	
					valueAdd = businessNum.multiply(businessRate);  //增加的值
					valueReduce = zero.subtract(businessNum); //减少的值
					break;
			}	
			if(valueAdd.compareTo(zero)<=0) //如果增加的值小于1则不进行操作
				throw new BusiException("没有达到最低兑换标准，兑换失败！");			
			
			TbbsBusiness tbbsBusiness = new TbbsBusiness();
			ParamUtils.DynaBinder(tbbsBusiness, params);
			tbbsBusiness.setBusinessDatetime(new Date());	
			
			switch(businessType)	
			{
				case 1:	//积分兑换金币					
					userDAO.doBusinessUpdate(userId, valueAdd, valueReduce, zero, zero);
					tbbsBusiness.setUserMoney(valueAdd);
					tbbsBusiness.setUserMark(valueReduce);	
					tbbsBusiness.setUserCredit(zero);
					tbbsBusiness.setUserDeposit(zero);			
					
					break;
				case 2: //金币兑换积分
					userDAO.doBusinessUpdate(userId, valueReduce, valueAdd, zero, zero);
					tbbsBusiness.setUserMoney(valueReduce);
					tbbsBusiness.setUserMark(valueAdd);	
					tbbsBusiness.setUserCredit(zero);
					tbbsBusiness.setUserDeposit(zero);					
					
					break;
				case 3:
					break;
				case 4:
					break;
				case 5:
					break;
			}			
			businessDAO.save(tbbsBusiness);			
		
	}
	

	public TbbsBusiness load(Integer id)
	{
		return (TbbsBusiness)businessDAO.load(id);
	}

	public void delete(Integer Id)
	{
			businessDAO.delete(Id);
	}

	
	public IPageList listPage(Integer userId,int pageIndex,int pageSize,String sort)
	{
		return businessDAO.listPage(userId,pageIndex, pageSize);
	}
	
	public void AutoBindingTable(Integer userId,javax.servlet.http.HttpServletRequest request)
	{
		Limit limit = ExtremeTableUtil.getLimit(request, Constants.DEFAULT_PAGE_SIZE);
		IPageList page = this.listPage(userId,limit.getPage(), limit.getCurrentRowsDisplayed(),ExtremeTableUtil.getSort(limit));
		request.setAttribute("resultList",page.getRsList());
		request.setAttribute("totalRows",page.getTotalSize());
	}


	public void setBusinessDAO(IBusinessDAO businessDAO) {
		this.businessDAO = businessDAO;
	}


	public void setUserDAO(IUserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
}